package com.mxin.ui.test

import android.view.View
import android.view.ViewGroup
import android.view.ViewStub
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.mxin.ui.R
import com.mxin.ui.base.BaseActivity
import com.mxin.ui.list.BaseListViewHelper
import com.mxin.ui.list.PageDataState
import com.mxin.ui.list.toPageData


class TestListActivity : BaseActivity() {

    private val listViewHelper = BaseListViewHelper<Aty>()
    private lateinit var recyclerView: RecyclerView

    override fun setContentView() {
        setContentView(R.layout.ui_activity_test_aty)
    }

    override fun showLoadingView(): View? = recyclerView


    override fun initView() {
        val titleStub = findViewById<ViewStub>(R.id.titleView)
        setTitleView(titleStub, "无分页List列表")
        val contentView = findViewById<ViewGroup>(R.id.layout_content)
        recyclerView = listViewHelper.bindListView(contentView){
            it.layoutManager = LinearLayoutManager(this)
            it.adapter = AtyAdapter(mutableListOf())
        }
        listViewHelper.subscribeData {
            when{
                it.state== PageDataState.Null ->{
                    showLoadFailed()
                }
                else ->{
                    showLoadSuccess()
                }
            }
        }
    }

    override fun initData() {
        showLoading()
        //延迟3秒加载
        recyclerView.postDelayed({
            listViewHelper.emit(initListData().toPageData())
        }, 3000)
    }

    override fun onLoadRetry() {
        initData()
    }

    private fun initListData():List<Aty>{
        val data = mutableListOf<Aty>()
        for(i in 0..100){
            data.add(Aty("描述花语：$i", TestListActivity::class.java))
        }
        return data
    }


}
